import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: { title: '系统概览' }
    },
    {
      path: '/employees',
      name: 'employees',
      redirect: '/employees/list',
      meta: { title: '员工管理' },
      children: [
        {
          path: 'list',
          name: 'employees-list',
          component: () => import('../views/employees/EmployeeList.vue'),
          meta: { title: '员工列表' }
        },
        {
          path: 'add',
          name: 'employees-add',
          component: () => import('../views/employees/EmployeeForm.vue'),
          meta: { title: '新增员工' }
        },
        {
          path: 'edit/:id',
          name: 'employees-edit',
          component: () => import('../views/employees/EmployeeForm.vue'),
          meta: { title: '编辑员工' }
        }
      ]
    },
    {
      path: '/rooms',
      name: 'rooms',
      redirect: '/rooms/list',
      meta: { title: '房间管理' },
      children: [
        {
          path: 'types',
          name: 'room-types',
          component: () => import('../views/rooms/RoomTypes.vue'),
          meta: { title: '房型管理' }
        },
        {
          path: 'list',
          name: 'rooms-list',
          component: () => import('../views/rooms/RoomList.vue'),
          meta: { title: '房间列表' }
        },
        {
          path: 'status',
          name: 'rooms-status',
          component: () => import('../views/rooms/RoomStatus.vue'),
          meta: { title: '房间状态' }
        }
      ]
    },
    {
      path: '/checkin',
      name: 'checkin',
      redirect: '/checkin/register',
      meta: { title: '入住管理' },
      children: [
        {
          path: 'register',
          name: 'checkin-register',
          component: () => import('../views/checkin/CheckinRegister.vue'),
          meta: { title: '快速入住' }
        },
        {
          path: 'checkout',
          name: 'checkin-checkout',
          component: () => import('../views/checkin/CheckinCheckout.vue'),
          meta: { title: '退房管理' }
        },
        {
          path: 'records',
          name: 'checkin-records',
          component: () => import('../views/checkin/CheckinRecords.vue'),
          meta: { title: '入住记录' }
        }
      ]
    },
    {
      path: '/reports',
      name: 'reports',
      redirect: '/reports/revenue',
      meta: { title: '数据统计' },
      children: [
        {
          path: 'revenue',
          name: 'reports-revenue',
          component: () => import('../views/reports/RevenueReport.vue'),
          meta: { title: '营收报表' }
        },
        {
          path: 'occupancy',
          name: 'reports-occupancy',
          component: () => import('../views/reports/OccupancyReport.vue'),
          meta: { title: '入住率统计' }
        },
        {
          path: 'export',
          name: 'reports-export',
          component: () => import('../views/reports/DataExport.vue'),
          meta: { title: '数据导出' }
        }
      ]
    },
    {
      path: '/settings',
      name: 'settings',
      redirect: '/settings/profile',
      meta: { title: '系统设置' },
      children: [
        {
          path: 'profile',
          name: 'settings-profile',
          component: () => import('../views/settings/UserProfile.vue'),
          meta: { title: '个人信息' }
        },
        {
          path: 'password',
          name: 'settings-password',
          component: () => import('../views/settings/ChangePassword.vue'),
          meta: { title: '修改密码' }
        },
        {
          path: 'system',
          name: 'settings-system',
          component: () => import('../views/settings/SystemConfig.vue'),
          meta: { title: '系统配置' }
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: { title: '关于系统' }
    },
    // 404页面
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFound.vue'),
      meta: { title: '页面未找到' }
    }
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 星级酒店管理系统`
  } else {
    document.title = '星级酒店管理系统'
  }

  next()
})

export default router
